package com.hjs.servlet.dailyinspection.renovatedetail;

import com.hjs.bean.RenovateDetail;
import com.hjs.mapper.DailyInspectRecordingMapper;
import com.hjs.tools.Mapper;
import com.hjs.tools.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;

/**
 * @author violetfreesia
 * @date 2020/6/19
 **/
@Slf4j
@WebServlet("/renovateDetail/add")
public class AddRenovateDetail extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        RequestUtil requestUtil = new RequestUtil(request, response);
        try (SqlSession sqlSession = Mapper.getSqlSession()) {
            DailyInspectRecordingMapper mapper = sqlSession.getMapper(DailyInspectRecordingMapper.class);
            RenovateDetail renovateDetail = new RenovateDetail(null, LocalDate.now()
                    , requestUtil.getInt("technicianID"), requestUtil.getString("remarks"), requestUtil.getInt("damageNoticeID"));
            boolean sqlResult = mapper.addOneRenovateDetail(renovateDetail);
            if (sqlResult) {
                sqlResult = mapper.updateDailyInspectionDamageNoticeRenovateStatusById(renovateDetail.getDamageNoticeID(),1,renovateDetail.getRenovateDetailID());
            }
            Mapper.SessionEnd(sqlResult, sqlSession, "添加整修详情失败");
            requestUtil.responseIsSuccess(sqlResult,"登记失败");
        } catch (Exception e) {
            log.error(e.toString());
            requestUtil.responseFail("未知错误");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        doPost(request, response);
    }
}
